import subprocess
import time
import sys

redis_host = ''

def redis_call(cmd):
    return subprocess.getstatusoutput('redis-cli -h ' + redis_host + ' ' + cmd)[1]

def wait(key, count):
    c = redis_call('get ' + key)
    while (c != count):
        print(key + ": " + c)
        time.sleep(1)
        c = redis_call('get ' + key)
    print(key + ": " + c)

redis_call('del ABSCount syncCount keyByAck')
parallelism = redis_call('get parallelism')
print('Parallelism: ' + parallelism)
barrier = redis_call('publish barrierChannel true')
print('Barrier True: ' + barrier)
wait('ABSCount', parallelism)
sync = redis_call('publish stateChannel sync')
print('Sync: ' + sync)
wait('syncCount', parallelism)
# prefetch = redis_call('publish stateChannel prefetch')
# print('Prefetch: ' + prefetch)
# wait('prefetchCount', parallelism)
keyBy = redis_call('publish keyByChannel type@' + sys.argv[1])
print('KeyByChannel: ' + keyBy)
wait('keyByAck', '3')
barrier = redis_call('publish barrierChannel false')
print('Barrier False: ' + barrier)
